主题
将JSON对象序列化为字符串 - JsonStringify
函数简介
将JSON对象序列化为字符串,支持格式化输出。
接口名称
JsonStringifyDLL调用
c
long JsonStringify(long obj, int indent, int* err);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| obj | 长整数型 | JSON对象句柄 |
| indent | 整数型 | 缩进空格数,0表示不格式化 |
| err | 整数型指针 | 错误码输出参数,可为0 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int64_t root = ola.JsonCreateObject();
if (root != 0) {
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
int32_t strErr = 0;
std::string jsonOut = ola.JsonStringify(root, 2, &strErr);
ola.JsonFree(root);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
long root = ola.JsonCreateObject();
if (root != 0)
{
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
int strErr;
string jsonOut = ola.JsonStringify(root, 2, out strErr);
ola.JsonFree(root);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
root = ola.JsonCreateObject()
if root != 0:
ola.JsonSetString(root, "username", "ola")
ola.JsonSetNumber(root, "level", 10)
json_out, str_err = ola.JsonStringify(root, 2)
ola.JsonFree(root)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long root = ola.JsonCreateObject();
if (root != 0) {
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
int strErr;
String jsonOut = ola.JsonStringify(root, 2, strErr);
ola.JsonFree(root);
}cpp
var ola = com("OlaPlug.OlaSoft")
var root = ola.JsonCreateObject()
if(root) {
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
var strErr = 0
var jsonOut = ola.JsonStringify(root, 2, strErr)
ola.JsonFree(root)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
root = ola.JsonCreateObject()
If root <> 0 Then
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
strErr = 0
jsonOut = ola.JsonStringify(root, 2, strErr)
ola.JsonFree(root)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
root = ola.JsonCreateObject ()
.如果真 (root ≠ 0)
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
strErr = 0
jsonOut = ola.JsonStringify (root, 2, strErr)
ola.JsonFree (root)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var root = ola.JsonCreateObject();
if(root){
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
var strErr = 0;
var jsonOut = ola.JsonStringify(root, 2, strErr);
ola.JsonFree(root);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 root = ola.JsonCreateObject()
如果真 (root ≠ 0)
{
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
整数 strErr = 0
文本型 jsonOut = ola.JsonStringify(root, 2, strErr)
ola.JsonFree(root)
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int64_t root = ola.JsonCreateObject();
if (root != 0) {
ola.JsonSetString(root, "username", "ola");
ola.JsonSetNumber(root, "level", 10);
int32_t strErr = 0;
std::string jsonOut = ola.JsonStringify(root, 2, &strErr);
ola.JsonFree(root);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long root = JsonCreateObject(instance);
if (root != 0) {
JsonSetString(instance, root, "username", "ola");
JsonSetBool(instance, root, "enabled", 1);
long ptr = JsonStringify(instance, root, 0, 0);
if (ptr != 0) {
char buffer[512] = {0};
GetStringFromPtr(ptr, buffer, sizeof(buffer));
FreeStringPtr(ptr);
}
JsonFree(instance, root);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long JsonCreateObject(long ola);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int JsonSetString(long ola, long obj, string key, string value);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int JsonSetBool(long ola, long obj, string key, int value);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long JsonStringify(long ola, long obj, int indent, int err);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int JsonFree(long ola, long obj);
long instance = CreateCOLAPlugInterFace();
long root = JsonCreateObject(instance);
if (root != 0) {
JsonSetString(instance, root, "username", "ola");
JsonSetBool(instance, root, "enabled", 1);
long ptr = JsonStringify(instance, root, 0, 0);
if (ptr != 0) {
StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
FreeStringPtr(ptr);
}
JsonFree(instance, root);
}python
root = ola.JsonCreateObject(instance)
if root:
ola.JsonSetString(instance, root, b"username", b"ola")
ola.JsonSetBool(instance, root, b"enabled", 1)
err = c_int(0)
ptr = ola.JsonStringify(instance, root, 0, err)
if ptr:
buf = create_string_buffer(512)
ola.GetStringFromPtr(ptr, buf, 512)
ola.FreeStringPtr(ptr)
ola.JsonFree(instance, root)返回值
返回JSON字符串指针,失败时返回0。需调用 FreeStringPtr 释放内存。
注意事项
- 返回的字符串需要调用 FreeStringPtr 释放内存
- indent 参数控制格式化,0表示紧凑格式,大于0表示缩进空格数
